﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormReiseDrawVerticalPipe : HYBaseForm
	{
		public event FormReiseDrawVerticalPipe.AuxiliaryFrameDelegate AuxiliaryFrameEvent;

		[DllImport("user32.dll")]
		public static extern bool SetForegroundWindow(IntPtr hWnd);

		public FormReiseDrawVerticalPipe(Autodesk.Revit.DB.Document doc, List<string> levelUp, List<string> pipeType, List<string> pipingSystemType, bool isHVAC)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.doc = doc;
			this.levelUp = levelUp;
			this.pipeType = pipeType;
			this.pipingSystemType = pipingSystemType;
			base.Icon = Resource.YArchitechIcon;
			this.osf = new OperateSegmentFitting(doc);
			this.isHVAC = isHVAC;
			base.Size = new Size(base.Size.Width, base.Size.Height - 23);
			this.flag = true;
		}

		public FormReiseDrawVerticalPipe(Autodesk.Revit.DB.Document doc, List<string> levelUp, List<string> pipeType, List<string> pipingSystemType, Pipe pipe)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.doc = doc;
			this.levelUp = levelUp;
			this.pipeType = pipeType;
			this.pipingSystemType = pipingSystemType;
			this.pipe = pipe;
			base.Icon = Resource.YArchitechIcon;
			this.osf = new OperateSegmentFitting(doc);
			this.flag = false;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (!this.onDraw(false))
			{
				return;
			}
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			this.gbLevel.Anchor = AnchorStyles.Bottom;
			this.gbPipe.Anchor = AnchorStyles.Bottom;
			this.hySystemType = new HYSystemType(this.doc, this.pipe);
			this.hySystemType.Location = new System.Drawing.Point(11, 15);
			this.hySystemType.SystemTypeChanged += this.hySystemType_SystemTypeChanged;
			this.hySystemType.btnPipeSettingChanged += this.hySystemType_btnPipeSettingChanged;
			this.gbPipeType.Controls.Add(this.hySystemType);
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			this.gbPipeType.Height = this.gbPipeType.Height - Convert.ToInt32((double)this.hySystemType.HeightLess * num2);
			base.Height -= Convert.ToInt32((double)this.hySystemType.HeightLess * num2);
			foreach (string item in this.levelUp)
			{
				this.comboBox1.Items.Add(item);
				this.comboBox2.Items.Add(item);
			}
			if (this.pipe == null)
			{
				this.SetDefaultNoPipe();
				this.isPostBack = false;
				this.isIni = true;
				this.isAuxiliaryFrame = this.checkBox3.Checked;
				this.hyTxtAngle.Enabled = this.chkLineAngle.Checked;
				this.SetLengthAndAngle();
				return;
			}
			if (this.pipe != null)
			{
				this.SetDefaultWithPipe();
				this.isPostBack = false;
				this.isIni = true;
				this.isAuxiliaryFrame = this.checkBox3.Checked;
				this.SetLengthAndAngle();
			}
		}

		private void SetLengthAndAngle()
		{
			FormReiseDrawVerticalPipe.length = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameLenght") == "") ? 0.0 : this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameLenght").ToDouble(0.0));
			FormReiseDrawVerticalPipe.angle = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameAngle") == "") ? 0.0 : this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrameAngle").ToDouble(0.0));
		}

		private void hySystemType_SystemTypeChanged(object sender, EventArgs e)
		{
			if (this.isIni && this.hySystemType.currentSystemTypeInfo != null)
			{
				this.comboBox_DN.Items.Clear();
				foreach (MEPSize mepsize in this.osf.GetPipeSegmentByPipeTypeName(this.hySystemType.currentSystemTypeInfo.PipeType).GetSizes())
				{
					this.comboBox_DN.Items.Add(Math.Ceiling(304.8 * mepsize.NominalDiameter).ToString());
				}
				if (this.comboBox_DN.Items.Count > 0)
				{
					this.comboBox_DN.SelectedItem = (this.comboBox_DN.Items.Contains(this.pipe_DN) ? this.pipe_DN : "");
					if (string.IsNullOrEmpty(this.comboBox_DN.Text))
					{
						this.comboBox_DN.SelectedIndex = 0;
					}
				}
			}
		}

		private void hySystemType_btnPipeSettingChanged(object sender, EventArgs e)
		{
			this.hySystemType.SaveSystemType();
			this.gbPipeType.Controls.Remove(this.hySystemType);
			this.hySystemType = new HYSystemType(this.doc, null);
			this.hySystemType.Location = new System.Drawing.Point(11, 15);
			this.hySystemType.SystemTypeChanged += this.hySystemType_SystemTypeChanged;
			this.hySystemType.btnPipeSettingChanged += this.hySystemType_btnPipeSettingChanged;
			this.gbPipeType.Controls.Add(this.hySystemType);
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isIni)
			{
				if (this.checkBox1.Checked)
				{
					this.label2.Text = "相对标高(m)";
					this.comboBox1.Enabled = true;
					this.textBoxOffsetDown.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) * 0.3048 - this.getLevelOffset(this.comboBox1.Text) * 0.3048, 3).ToString();
					return;
				}
				this.label2.Text = "绝对标高(m)";
				this.comboBox1.Enabled = false;
				this.textBoxOffsetDown.Text = Math.Round((this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) + this.getLevelOffset(this.comboBox1.Text)) * 0.3048, 3).ToString();
			}
		}

		private void checkBox2_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isIni)
			{
				if (this.checkBox2.Checked)
				{
					this.label4.Text = "相对标高(m)";
					this.comboBox2.Enabled = true;
					this.textBoxOffsetUp.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) * 0.3048 - this.getLevelOffset(this.comboBox2.Text) * 0.3048, 3).ToString();
					return;
				}
				this.label4.Text = "绝对标高(m)";
				this.comboBox2.Enabled = false;
				this.textBoxOffsetUp.Text = Math.Round((this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) + this.getLevelOffset(this.comboBox2.Text)) * 0.3048, 3).ToString();
			}
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.checkBox1.Checked)
			{
				this.textBoxOffsetDown.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) * 0.3048 + this.getLevelOffset(this.lastLevel1) * 0.3048 - this.getLevelOffset(this.comboBox1.Text) * 0.3048, 3).ToString();
				this.lastLevel1 = this.comboBox1.Text;
			}
		}

		private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.checkBox2.Checked)
			{
				this.textBoxOffsetUp.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) * 0.3048 + this.getLevelOffset(this.lastLevel2) * 0.3048 - this.getLevelOffset(this.comboBox2.Text) * 0.3048, 3).ToString();
				this.lastLevel2 = this.comboBox2.Text;
			}
		}

		private void comboBox1_DropDown(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBox1);
		}

		private void comboBox2_DropDown(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBox2);
		}

		private void comboBox_DN_TextChanged(object sender, EventArgs e)
		{
			AuxiliaryFrameArgs auxiliaryFrameArgs = new AuxiliaryFrameArgs();
			auxiliaryFrameArgs.Type = senderType.diameter;
			if (this.AuxiliaryFrameEvent != null)
			{
				this.AuxiliaryFrameEvent(((System.Windows.Forms.ComboBox)sender).SelectedItem.ToString(), auxiliaryFrameArgs);
			}
		}

		private void checkBox3_CheckedChanged(object sender, EventArgs e)
		{
			this.bCheck = ((CheckBox)sender).Checked;
			this.btnShowAuxiliaryFrame.Enabled = this.bCheck;
			this.isAuxiliaryFrame = this.bCheck;
			if (this.AuxiliaryFrameEvent != null)
			{
				AuxiliaryFrameArgs auxiliaryFrameArgs = new AuxiliaryFrameArgs();
				auxiliaryFrameArgs.Type = senderType.check;
				this.AuxiliaryFrameEvent(this.isAuxiliaryFrame, auxiliaryFrameArgs);
			}
			if (this.AuxiliaryFrameEvent != null)
			{
				AuxiliaryFrameArgs auxiliaryFrameArgs2 = new AuxiliaryFrameArgs();
				auxiliaryFrameArgs2.Type = senderType.length;
				this.AuxiliaryFrameEvent(FormReiseDrawVerticalPipe.length, auxiliaryFrameArgs2);
			}
			if (this.AuxiliaryFrameEvent != null)
			{
				AuxiliaryFrameArgs auxiliaryFrameArgs3 = new AuxiliaryFrameArgs();
				auxiliaryFrameArgs3.Type = senderType.angle;
				this.AuxiliaryFrameEvent(FormReiseDrawVerticalPipe.angle, auxiliaryFrameArgs3);
			}
		}

		private void FormReiseDrawVerticalPipe_MouseLeave(object sender, EventArgs e)
		{
			FormReiseDrawVerticalPipe.SetForegroundWindow(new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle).Handle);
		}

		private bool RegexText(string strText)
		{
			return Regex.IsMatch(strText, "^[0-9a-zA-Z'_]+$");
		}

		private void SetDefaultNoPipe()
		{
			this.GetUserData();
			this.comboBox_DN.Items.Clear();
			foreach (MEPSize mepsize in this.osf.GetPipeSegmentByPipeTypeName(this.hySystemType.currentSystemTypeInfo.SysType).GetSizes())
			{
				this.comboBox_DN.Items.Add(Math.Ceiling(304.7 * mepsize.NominalDiameter).ToString());
			}
			if (this.comboBox_DN.Items.Count > 0)
			{
				this.comboBox_DN.SelectedItem = (this.comboBox_DN.Items.Contains(this.pipe_DN) ? this.pipe_DN : "");
				if (string.IsNullOrEmpty(this.comboBox_DN.Text))
				{
					this.comboBox_DN.SelectedIndex = 0;
				}
			}
			this.SetDefaltValue();
		}

		private void SetDefaultWithPipe()
		{
			this.Text = "立管参数修改";
			this.label2.Text = "相对标高(m)";
			this.label4.Text = "相对标高(m)";
			Level level = this.pipe.Document.GetElementById(this.pipe.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId()) as Level;
			if (level == null)
			{
				level = YJKRevitTools.GetLevelClose((this.pipe.Location as LocationCurve).Curve.GetEndPoint(0), this.doc);
			}
			this.getLevelOffset(level.Name);
			if (this.pipe.GetParameter(BuiltInParameter.RBS_START_OFFSET_PARAM).DisplayUnitType == null)
			{
				this.textBoxOffsetDown.Text = double.Parse(this.pipe.GetParameter(BuiltInParameter.RBS_START_OFFSET_PARAM).AsValueString()).ToString();
				this.textBoxOffsetUp.Text = double.Parse(this.pipe.GetParameter(BuiltInParameter.RBS_END_OFFSET_PARAM).AsValueString()).ToString();
			}
			else
			{
				this.textBoxOffsetDown.Text = (double.Parse(this.pipe.GetParameter(BuiltInParameter.RBS_START_OFFSET_PARAM).AsValueString()) / 1000.0).ToString();
				this.textBoxOffsetUp.Text = (double.Parse(this.pipe.GetParameter(BuiltInParameter.RBS_END_OFFSET_PARAM).AsValueString()) / 1000.0).ToString();
			}
			this.comboBox1.SelectedItem = level.Name;
			this.comboBox2.SelectedItem = level.Name;
			this.checkBox1.Checked = true;
			this.checkBox2.Checked = true;
			this.lastLevel1 = this.comboBox1.Text;
			this.lastLevel2 = this.comboBox2.Text;
			this.btnOk.Visible = true;
			this.btnCancel.Visible = true;
			this.comboBox_DN.Items.Clear();
			foreach (MEPSize mepsize in (this.doc.GetElementById(this.pipe.GetParameter(BuiltInParameter.RBS_PIPE_SEGMENT_PARAM).AsElementId()) as PipeSegment).GetSizes())
			{
				this.comboBox_DN.Items.Add(Math.Ceiling(304.7 * mepsize.NominalDiameter).ToString());
			}
			this.comboBox_DN.Text = (this.pipe.Diameter * 304.8).ToString();
			this.operation = new AuxiliaryFrameOperation();
			string text = this.operation.ReadAuxiliaryFrameLength(this.pipe);
			if (string.IsNullOrEmpty(text) || text == "0")
			{
				FormReiseDrawVerticalPipe.length = 0.0;
				this.checkBox3.Checked = false;
			}
			else
			{
				FormReiseDrawVerticalPipe.length = text.ToDouble(0.0);
				this.checkBox3.Checked = true;
			}
			Parameter parameter = this.pipe.GetParameter("区号楼号");
			if (parameter != null)
			{
				string[] districtBuild = this.GetDistrictBuild(parameter.AsString());
				if (districtBuild[0] != "")
				{
					this.District = true;
					this.DistrictNum = districtBuild[0];
				}
				else
				{
					this.District = false;
					this.DistrictNum = "";
				}
				if (districtBuild[1] != "")
				{
					this.Build = true;
					this.BuildNum = districtBuild[1];
				}
				else
				{
					this.Build = false;
					this.BuildNum = "";
				}
			}
			parameter = this.pipe.GetParameter("立管编号");
			if (parameter != null)
			{
				this.hyTextBox1.Text = parameter.AsString();
			}
			else
			{
				this.hyTextBox1.Text = "";
			}
			this.chkLineAngle.Checked = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "chkLineAngle") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "chkLineAngle")));
			this.hyTxtAngle.Text = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "LineAngle") == "") ? "90" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "LineAngle"));
			this.chkLineAngle.Enabled = false;
			this.hyTxtAngle.Enabled = false;
		}

		private string[] GetDistrictBuild(string strDistrictBuild)
		{
			string[] array = new string[2];
			if (string.IsNullOrEmpty(strDistrictBuild))
			{
				array[0] = "";
				array[1] = "";
			}
			else if (strDistrictBuild.Split(new char[]
			{
				'-'
			}).Count<string>() == 2)
			{
				if (Regex.IsMatch(strDistrictBuild.Replace("-", ""), "^[+-]?[0-9]+\\d*[.]?\\d*$"))
				{
					array[0] = "";
					array[1] = strDistrictBuild.Replace("-", "");
				}
				else
				{
					array[0] = strDistrictBuild.Replace("-", "");
					array[1] = "";
				}
			}
			else if (strDistrictBuild.Split(new char[]
			{
				'-'
			}).Count<string>() == 3)
			{
				array[0] = strDistrictBuild.Split(new char[]
				{
					'-'
				})[0];
				array[1] = strDistrictBuild.Split(new char[]
				{
					'-'
				})[1];
			}
			return array;
		}

		private void GetUserData()
		{
			string[] riserPipeText = this.userSetting.GetRiserPipeText();
			this.pipe_DN = riserPipeText[2];
			this.comboBox_DN.Text = (this.comboBox_DN.Items.Contains(riserPipeText[2]) ? riserPipeText[2] : "");
			this.comboBox1.SelectedItem = (this.comboBox1.Items.Contains(riserPipeText[6]) ? riserPipeText[6] : "");
			this.comboBox2.SelectedItem = (this.comboBox2.Items.Contains(riserPipeText[7]) ? riserPipeText[7] : "");
			this.lastLevel1 = riserPipeText[6];
			this.lastLevel2 = riserPipeText[7];
			this.textBoxOffsetDown.Text = (string.IsNullOrEmpty(riserPipeText[3]) ? "0" : riserPipeText[3]);
			this.textBoxOffsetUp.Text = (string.IsNullOrEmpty(riserPipeText[4]) ? "0" : riserPipeText[4]);
			if (this.lastLevel1 == this.lastLevel2 && this.textBoxOffsetDown.Text == this.textBoxOffsetUp.Text)
			{
				this.textBoxOffsetUp.Text = (this.textBoxOffsetDown.Text.ToDouble(0.0) + 3.0).ToString();
			}
			this.chkLineAngle.Checked = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "chkLineAngle") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "chkLineAngle")));
			this.checkBox3.Checked = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "IsAuxiliaryFrame") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "IsAuxiliaryFrame")));
			this.checkBox2.Checked = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrame") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "AuxiliaryFrame")));
			this.hyTextBox1.Text = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "CustomNum") == "") ? "1" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "CustomNum"));
			this.hyTxtAngle.Text = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "LineAngle") == "") ? "90" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "LineAngle"));
			this.District = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "District") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "District")));
			this.Build = (!(this.userSetting.ReadValue("DrawVerticalPipeSetting", "Build") == "") && Convert.ToBoolean(this.userSetting.ReadValue("DrawVerticalPipeSetting", "Build")));
			this.DistrictNum = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "DistrictNum") == "") ? "A" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "DistrictNum"));
			this.BuildNum = ((this.userSetting.ReadValue("DrawVerticalPipeSetting", "BuildNum") == "") ? "1" : this.userSetting.ReadValue("DrawVerticalPipeSetting", "BuildNum"));
			if (riserPipeText[5] == "11")
			{
				this.checkBox1.Checked = true;
				this.checkBox2.Checked = true;
				this.label2.Text = "相对标高(m)";
				this.comboBox1.Enabled = true;
				this.label4.Text = "相对标高(m)";
				this.comboBox2.Enabled = true;
			}
			else if (riserPipeText[5] == "00")
			{
				this.checkBox1.Checked = false;
				this.checkBox2.Checked = false;
				this.label2.Text = "绝对标高(m)";
				this.comboBox1.Enabled = false;
				this.label4.Text = "绝对标高(m)";
				this.comboBox2.Enabled = false;
			}
			else if (riserPipeText[5] == "01")
			{
				this.checkBox1.Checked = false;
				this.checkBox2.Checked = true;
				this.label2.Text = "绝对标高(m)";
				this.comboBox1.Enabled = false;
				this.label4.Text = "相对标高(m)";
				this.comboBox2.Enabled = true;
			}
			else if (riserPipeText[5] == "10")
			{
				this.checkBox1.Checked = true;
				this.checkBox2.Checked = false;
				this.label2.Text = "相对标高(m)";
				this.comboBox1.Enabled = true;
				this.label4.Text = "绝对标高(m)";
				this.comboBox2.Enabled = false;
			}
			if (string.IsNullOrEmpty(this.comboBox1.Text))
			{
				this.lastLevel1 = this.comboBox1.Items[0].ToString();
				if (this.comboBox1.Items != null && this.comboBox1.Items.Count > 0)
				{
					this.comboBox1.SelectedItem = this.comboBox1.Items[0];
				}
			}
			if (string.IsNullOrEmpty(this.comboBox2.Text))
			{
				this.lastLevel2 = this.comboBox2.Items[0].ToString();
				if (this.comboBox2.Items != null && this.comboBox2.Items.Count > 0)
				{
					this.comboBox2.SelectedItem = this.comboBox2.Items[0];
				}
			}
		}

		private void SetUserData()
		{
			string[] array = new string[8];
			array[2] = this.comboBox_DN.Text;
			array[3] = this.textBoxOffsetDown.Text;
			array[4] = this.textBoxOffsetUp.Text;
			array[0] = this.hySystemType.currentSystemTypeInfo.PipeType;
			array[1] = this.hySystemType.currentSystemTypeInfo.SysType;
			if (this.checkBox1.Checked && this.checkBox2.Checked)
			{
				array[5] = "11";
			}
			else if (this.checkBox1.Checked && !this.checkBox2.Checked)
			{
				array[5] = "10";
			}
			else if (!this.checkBox1.Checked && this.checkBox2.Checked)
			{
				array[5] = "01";
			}
			else if (!this.checkBox1.Checked && !this.checkBox2.Checked)
			{
				array[5] = "00";
			}
			array[6] = this.comboBox1.Text;
			array[7] = this.comboBox2.Text;
			this.userSetting.WriteRiserPipeText(array);
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "AuxiliaryFrame", this.checkBox2.Checked.ToString());
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "PipeNum", "false");
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "CustomNum", this.hyTextBox1.Text.ToString());
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "IsAuxiliaryFrame", this.checkBox3.Checked.ToString());
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "chkLineAngle", this.chkLineAngle.Checked.ToString());
			this.userSetting.WriteValue("DrawVerticalPipeSetting", "LineAngle", this.hyTxtAngle.Text.ToString());
		}

		private string GetDefaultTxtValue(string txtValue)
		{
			if (txtValue == "" || !Regex.IsMatch(txtValue, "^[0-9a-zA-Z'_]+$"))
			{
				txtValue = "1";
			}
			return txtValue;
		}

		private double getLevelOffset(string level)
		{
			return this.Get_Level(this.doc, level).ProjectElevation;
		}

		public bool onDraw(bool isDefalt = true)
		{
			if (this.pipe == null)
			{
				if (!Regex.IsMatch(this.comboBox_DN.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
				{
					YJKMessageBox.Show("直径必须为有效数字!");
					this.comboBox_DN.Focus();
					return false;
				}
			}
			else if (!Regex.IsMatch(this.comboBox_DN.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("直径必须为有效数字!");
				this.comboBox_DN.Focus();
				return false;
			}
			if (!Regex.IsMatch(this.hyTextBox1.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Show("立管编号不能为空且必须由英文字母、数字或'_组成！");
				this.hyTextBox1.Focus();
				this.hyTextBox1.SelectAll();
				base.Focus();
				return false;
			}
			if (!Regex.IsMatch(this.textBoxOffsetDown.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("标高必须为有效数字!");
				this.textBoxOffsetDown.Focus();
				this.textBoxOffsetDown.SelectAll();
				base.Focus();
				return false;
			}
			if (!Regex.IsMatch(this.textBoxOffsetUp.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("标高必须为有效数字!");
				this.textBoxOffsetUp.Focus();
				this.textBoxOffsetUp.SelectAll();
				base.Focus();
				return false;
			}
			if (this.chkLineAngle.Checked && !Regex.IsMatch(this.hyTxtAngle.Text, "^\\+?[0-9][0-9]*$"))
			{
				YJKMessageBox.Show("引线角度必须为正整数!");
				this.hyTxtAngle.Focus();
				this.hyTxtAngle.SelectAll();
				base.Focus();
				return false;
			}
			string text;
			if (this.pipe == null)
			{
				text = this.comboBox_DN.Text;
			}
			else
			{
				text = this.comboBox_DN.Text;
			}
			if (!(this.textBoxOffsetDown.Text != "") || !(this.textBoxOffsetUp.Text != "") || !(text != ""))
			{
				YJKMessageBox.Warning("偏移量和直径都不能为空!");
				return false;
			}
			if (double.Parse(text) <= 5.0 || double.Parse(text) >= 5000.0)
			{
				YJKMessageBox.Warning("直径应大于5，小于5000!");
				this.comboBox_DN.Focus();
				return false;
			}
			double num;
			if (this.checkBox1.Checked)
			{
				num = this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) + this.getLevelOffset(this.comboBox1.Text);
			}
			else
			{
				num = this.zCoord(double.Parse(this.textBoxOffsetDown.Text));
			}
			double num2;
			if (this.checkBox2.Checked)
			{
				num2 = this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) + this.getLevelOffset(this.comboBox2.Text);
			}
			else
			{
				num2 = this.zCoord(double.Parse(this.textBoxOffsetUp.Text));
			}
			PipeType pipeType = this.Get_PipeType(this.doc, this.hySystemType.currentSystemTypeInfo.PipeType);
			PipingSystemType pipingSystemType = this.Get_PipingSystemType(this.doc, this.hySystemType.currentSystemTypeInfo.SysType);
			this.zStart = num;
			this.zEnd = num2;
			if (Math.Abs(num - num2) * 0.3048 > 0.01)
			{
				this.pipeTypeRiser = pipeType;
				this.pipingSystemTypeRiser = pipingSystemType;
				this.diameter = text;
				if (isDefalt)
				{
					this.SetUserData();
				}
				base.DialogResult = DialogResult.OK;
				return true;
			}
			YJKMessageBox.Warning("立管起始点相距太近，不能小于0.01米!");
			return false;
		}

		private double zCoord(double offset)
		{
			return offset / 0.3048;
		}

		private static bool IsNum(string str)
		{
			if (!string.IsNullOrEmpty(str))
			{
				for (int i = 0; i < str.Length; i++)
				{
					if (str[i] < '0' || str[i] > '9')
					{
						return false;
					}
				}
				return true;
			}
			return false;
		}

		private string Get_MaxPipeLabel(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Pipe), document);
			List<int> list = new List<int>();
			list.Add(0);
			foreach (Element element in elementByClass)
			{
				if (FormReiseDrawVerticalPipe.IsNum((element as Pipe).GetParameter(BuiltInParameter.DOOR_NUMBER).AsString()))
				{
					list.Add(int.Parse((element as Pipe).GetParameter(BuiltInParameter.DOOR_NUMBER).AsString()));
				}
			}
			return (list.Max() + 1).ToString();
		}

		private PipeType Get_PipeType(Autodesk.Revit.DB.Document document, string pipeTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipeType), document);
			return this.GetElementSameName(elementByClass, pipeTypeName) as PipeType;
		}

		private PipingSystemType Get_PipingSystemType(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipingSystemType), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as PipingSystemType;
		}

		private Level Get_Level(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Level), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as Level;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private void AdjustComboBoxDropDownListWidth(object comboBox)
		{
			Graphics graphics = null;
			try
			{
				System.Windows.Forms.ComboBox comboBox2 = null;
				if (comboBox is System.Windows.Forms.ComboBox)
				{
					comboBox2 = (System.Windows.Forms.ComboBox)comboBox;
				}
				else
				{
					if (!(comboBox is ToolStripComboBox))
					{
						return;
					}
					comboBox2 = ((ToolStripComboBox)comboBox).ComboBox;
				}
				int num = comboBox2.Width;
				graphics = comboBox2.CreateGraphics();
				Font font = comboBox2.Font;
				int num2 = (comboBox2.Items.Count > comboBox2.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
				foreach (object obj in comboBox2.Items)
				{
					if (obj != null)
					{
						int num3 = (int)graphics.MeasureString(obj.ToString().Trim(), font).Width + num2;
						if (num < num3)
						{
							num = num3;
						}
					}
				}
				comboBox2.DropDownWidth = num;
			}
			catch
			{
			}
			finally
			{
				if (graphics != null)
				{
					graphics.Dispose();
				}
			}
		}

		private void SetDefaltValue()
		{
			if (this.comboBox_DN.Text == "")
			{
				return;
			}
			if (this.userSetting.ReadValue("DrawVerticalPipeSetting", "LineAngle") != "")
			{
				this.hyTxtAngle.Text = this.userSetting.ReadValue("DrawVerticalPipeSetting", "LineAngle");
				return;
			}
		}

		private void hyTextBox4_TextChanged(object sender, EventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox.Text == "" || !Regex.IsMatch(hytextBox.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				hytextBox.Focus();
				hytextBox.SelectAll();
				return;
			}
		}

		private void FormReiseDrawVerticalPipe_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.flag)
			{
				this.hySystemType.SaveSystemType();
				FormReiseDrawVerticalPipe.SetForegroundWindow(new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle).Handle);
				this.SimulationPressESC();
				return;
			}
			this.hySystemType.SaveSystemType();
			FormReiseDrawVerticalPipe.SetForegroundWindow(new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle).Handle);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormReiseDrawVerticalPipe.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormReiseDrawVerticalPipe.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FormReiseDrawVerticalPipe_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SetUserData();
		}

		private void comboBox_DN_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.pipe_DN = this.comboBox_DN.Text;
		}

		private void hyTextBox1_Validating(object sender, CancelEventArgs e)
		{
			if (!Regex.IsMatch(this.hyTextBox1.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				this.hyTextBox1.Focus();
				this.hyTextBox1.SelectAll();
				return;
			}
		}

		private void hyTextBox2_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox.Text == "" || !Regex.IsMatch(textBox.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				textBox.Focus();
				textBox.SelectAll();
				return;
			}
		}

		private void hyTextBox2_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = (System.Windows.Forms.TextBox)sender;
			if (!Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-zA-Z'_\\b]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				textBox.Focus();
				e.Handled = true;
			}
		}

		private void hyTextBox5_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = (System.Windows.Forms.TextBox)sender;
			if (!Regex.IsMatch(e.KeyChar.ToString(), "^[0-9\\b]+$"))
			{
				YJKMessageBox.Warning("文本框请输入数字！");
				textBox.Focus();
				e.Handled = true;
			}
		}

		private void btnShowAuxiliaryFrame_Click(object sender, EventArgs e)
		{
			FormReiseAuxiliaryFrame formReiseAuxiliaryFrame = new FormReiseAuxiliaryFrame(this.isHVAC, this.isAuxiliaryFrame);
			formReiseAuxiliaryFrame.lengthAndAngleChanged += this.frm_lengthAndAngleChanged;
			formReiseAuxiliaryFrame.Location = new System.Drawing.Point(base.Location.X + base.Size.Width, base.Location.Y + 470 - formReiseAuxiliaryFrame.Size.Height);
			formReiseAuxiliaryFrame.ShowDialog();
		}

		private void frm_lengthAndAngleChanged(object sender, EventArgs e)
		{
			if (this.AuxiliaryFrameEvent != null)
			{
				AuxiliaryFrameArgs auxiliaryFrameArgs = new AuxiliaryFrameArgs();
				auxiliaryFrameArgs.Type = senderType.length;
				this.AuxiliaryFrameEvent(FormReiseDrawVerticalPipe.length, auxiliaryFrameArgs);
			}
			if (this.AuxiliaryFrameEvent != null)
			{
				AuxiliaryFrameArgs auxiliaryFrameArgs2 = new AuxiliaryFrameArgs();
				auxiliaryFrameArgs2.Type = senderType.angle;
				this.AuxiliaryFrameEvent(FormReiseDrawVerticalPipe.angle, auxiliaryFrameArgs2);
			}
		}

		private void chkLineAngle_CheckedChanged(object sender, EventArgs e)
		{
			this.hyTxtAngle.Enabled = this.chkLineAngle.Checked;
		}

		private void hyTxtAngle_Validating(object sender, CancelEventArgs e)
		{
			if (string.IsNullOrEmpty(this.hyTxtAngle.Text))
			{
				return;
			}
			try
			{
				double num = Convert.ToDouble(this.hyTxtAngle.Text);
				if (num < 0.0 || num > 90.0)
				{
					YJKMessageBox.Show("引线角度必须为0°~90°的正整数。");
					this.hyTxtAngle.Text = MarkRiserPipeData.Instance.Angle;
				}
			}
			catch (Exception)
			{
			}
		}

		private void btnNum_Click(object sender, EventArgs e)
		{
			FormReiseNum formReiseNum = new FormReiseNum(this);
			formReiseNum.Location = new System.Drawing.Point(base.Location.X + base.Size.Width, base.Location.Y + 430 - formReiseNum.Size.Height);
			formReiseNum.ShowDialog();
		}

		private void hyTextBox1_TextChanged(object sender, EventArgs e)
		{
			FormReiseDrawVerticalPipe.resiePipeNumber = this.hyTextBox1.Text;
		}

		private void hyTxtAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private Document doc;

		private List<string> levelUp = new List<string>();

		private List<string> pipeType = new List<string>();

		private List<string> pipingSystemType = new List<string>();

		public double zStart;

		public double zEnd;

		public string labelPipe;

		private Pipe pipe;

		public PipeType pipeTypeRiser;

		public PipingSystemType pipingSystemTypeRiser;

		public string diameter = "0";

		private HVACUserSetting userSetting = new HVACUserSetting();

		private OperateSegmentFitting osf;

		private bool isIni;

		public bool isAuxiliaryFrame = true;

		public bool bCheck;

		public static double length = 0.0;

		public static double angle = 0.0;

		public double leadLineAngle;

		public static string resiePipeNumber = "1";

		private string lastLevel1 = "";

		private string lastLevel2 = "";

		private AuxiliaryFrameOperation operation;

		public string strBuild = "";

		public string strDistrict = "";

		private bool isHVAC = true;

		private string pipe_DN = "";

		public static string strSelectedSystemType = "";

		private bool flag;

		public HYSystemType hySystemType;

		public bool District;

		public bool Build;

		public string DistrictNum = "";

		public string BuildNum = "";

		private bool isPostBack = true;

		public delegate void AuxiliaryFrameDelegate(object sender, AuxiliaryFrameArgs e);
	}
}
